//获取按钮
let btns = document.querySelectorAll("#btns")
console.log(btns)
console.log(btns[1])
btns[1].onclick = function () {
  location.href = "reg.html"
}

let ipts = document.querySelectorAll(".rgeIpt")
console.log(ipts)
let regItP1 = document.querySelector(".regItP1")
let regItP2 = document.querySelector(".regItP2")

//判断正则
let usernameFly
ipts[0].addEventListener("change", function () {
  let username = ipts[0].value
  console.log("111")
  let usernameReg = /^[a-z]\w{2,16}$/i
  if (usernameReg.test(username)) {
    usernameFly = true
    regItP1.style.display = "block"
    regItP1.innerHTML = "用户账号格式正确"
    regItP1.style.color = "green"
    ipts[0].classList.add("activeTus")
    ipts[0].classList.remove("activeFas")
  } else {
    usernameFly = false
    ipts[0].classList.remove("activeTus")
    ipts[0].classList.add("activeFas")
    regItP1.innerHTML = "用户账号格式不正确，字母开头3到16位"
    regItP1.style.display = "block"
    regItP1.style.color = "red"
  }
  btnStatue()
})
//密码
let passwordFly
ipts[1].addEventListener("change", function () {
  let username = ipts[1].value
  console.log("222")
  let usernameReg = /^[a-zA-Z]\w{5,17}$/
  if (usernameReg.test(username)) {
    passwordFly = true
    regItP2.style.display = "block"
    regItP2.innerHTML = "密码格式正确"
    regItP2.style.color = "green"
    ipts[1].classList.add("activeTus")
    ipts[1].classList.remove("activeFas")
  } else {
    passwordFly = false
    ipts[1].classList.remove("activeTus")
    ipts[1].classList.add("activeFas")
    regItP2.innerHTML = "密码格式不正确，字母开头3到16位"
    regItP2.style.display = "block"
    regItP2.style.color = "red"
  }
  btnStatue()
})

//登录下次自动登录
let username = localStorage.getItem("username")
console.log(username)
ipts[0].value = username
let password = localStorage.getItem("password")
ipts[1].value = password
if (ipts[0].value != null && ipts[1].value != null) {
  usernameFly = true
  passwordFly = true
  btnStatue()
}
//判断 登录
function btnStatue() {
  if (usernameFly && passwordFly) {
    btns[0].disabled = false
  } else {
    btns[0].disabled = true
  }
  console.log(usernameFly, passwordFly)
}

// ipts[0].onfocus = function () {
//   regItP1.style.opacity = "0"
// }
// ipts[1].onfocus = function () {
//   regItP2.style.opacity = "0"
// }

//下次自动登录
let conCeck = document.querySelector(".consentCheck")
console.log(conCeck)
console.log(conCeck.checked)
conCeck.addEventListener("click", function () {
  let username = ipts[0].value
  let password = ipts[1].value
  console.log(username, password)
  if (conCeck.checked == true) {
    //存入本地
    conCeck.checked == true
    localStorage.setItem("username", username)
    localStorage.setItem("password", password)
  } else {
    //删除本地
    conCeck.checked == false
    localStorage.removeItem("username")
    localStorage.removeItem("password")
  }
})

btns[0].addEventListener("click", function () {
  let username = ipts[0].value
  let password = ipts[1].value
  if (username != "" && username != "") {
    let loginUrl = `http://jx.xuzhixiang.top/ap/api/login.php`
    let p = axios.get(loginUrl, { params: { username, password } })
    p.then(function (r) {
      if (r.data.code == 1) {
        console.log(r)
        localStorage.setItem("id", r.data.data.id)
        localStorage.setItem("token", r.data.data.token)
        localStorage.setItem("username", username)
        location.href = "index.html"
      } else {
        regItP1.innerHTML = r.data.data
        regItP1.style.opacity = "1"
      }
    })
  } else {
    alert("请输入账号")
  }
})
